package cn.farm.farm.Aop;

import cn.farm.farm.Util.UserUtil;
import cn.farm.farm.Util.redis.JedisAdapter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

@Component
@Aspect
@Slf4j
public class TestUpdatePVAndUv {


    @Autowired
    JedisAdapter jedisAdapter;

    @Value("${Jedis.PV}")
    String PV;

    @Value("${Jedis.UV}")
    String UV;

    @Pointcut("@annotation(cn.farm.farm.Aop.UpdatePVAndUv)")
    public void updatePVAndUv(){}


    @Before("updatePVAndUv()")
    public void before(JoinPoint joinPoint){
        Object[] objects = joinPoint.getArgs();
        HttpSession session = (HttpSession) objects[0];
        int userID = UserUtil.getUserID(session);
        if (userID != -1){
            jedisAdapter.pfadd(UV,userID+"");
        }
        jedisAdapter.incr(PV);
    }
}
